import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author LKQ
 * @date 2021/12/25 10:25
 * @description 可以不用list维护值。
 */
public class FormalSolution {
    public static void main(String[] args) {
        TreeNode t12 = new TreeNode(12), t8 = new TreeNode(8), t6 = new TreeNode(6)
                ,t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3, t12, t8), t7 = new TreeNode(7, t6, null),
                t9 = new TreeNode(9, null, t2);
        TreeNode t10 = new TreeNode(10, t3, null), t4 = new TreeNode(4, t7, t9);
        TreeNode root = new TreeNode(1, t10, t4);
        FormalSolution solution = new FormalSolution();
        solution.isEvenOddTree(root);
    }

    public boolean isEvenOddTree(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.offer(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            int prev = level % 2 == 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                int value = node.val;
                if (level % 2 == value % 2) {
                    return false;
                }
                if ((level % 2 == 0 && value <= prev) || (level % 2 == 1 && value >= prev)) {
                    return false;
                }
                prev = value;
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            level++;
        }
        return true;
    }

}
